import ccclass = cc._decorator.ccclass;
import Drawable from "../Domain/Drawable";

@ccclass
export default class DrawableContainer extends cc.Component {

    private graphic: cc.Graphics = null

    private drawable: Drawable = null

    onLoad() {
        this.node.setAnchorPoint(0, 0)
    }

    public start() {
        if (!this.graphic) {
            this.graphic = this.addComponent(cc.Graphics)
        } else {
            this.graphic = this.getComponent(cc.Graphics)
        }
    }

    public init(drawable: Drawable) {
        this.drawable = drawable
        this.node.width = this.drawable.bound[0]
        this.node.height = this.drawable.bound[1]

        this.updateUi()
    }

    updateUi() {
        this.graphic.clear(true)
        this.graphic.strokeColor = cc.Color.RED
        this.graphic.lineCap = cc.Graphics.LineCap.ROUND
        this.graphic.lineJoin = cc.Graphics.LineJoin.ROUND
        this.graphic.lineWidth = 5

        let p = null
        for (let i = 0; i < this.drawable.path.length; i++) {
            p = this.drawable.path[i]
            if (i == 0) {
                this.graphic.moveTo(p.x, p.y)
            } else {
                this.graphic.lineTo(p.x, p.y)
            }
        }
        this.graphic.close()
        this.graphic.stroke()
    }

}